package com.deng.androidwork6;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.deng.androidwork6.framgments.BlankFragment;
import com.deng.androidwork6.framgments.BlankFragment2;
import com.deng.androidwork6.framgments.BlankFragment3;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //准备数据
    ArrayList<Fragment> fragments;

    //定义适配器
    FragmentPagerAdapter adapter;
    //应用适配器
    ViewPager mViewPager;//界面上的viewpager对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();//构建framgments
        initAdapter();
        mViewPager =findViewById(R.id.vp_framgment);
        mViewPager.setAdapter(adapter);
    }

    private void initAdapter() {
        adapter =new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
    }

    private void initData() {
        fragments=new ArrayList<>();
        fragments.add(new BlankFragment());
        fragments.add(new BlankFragment2());
        fragments.add(new BlankFragment3());
    }

    public void tab2(View view) {
        mViewPager.setCurrentItem(1);
    }

    public void tab1(View view) {
        mViewPager.setCurrentItem(2);
    }

    public void tab3(View view) {
        mViewPager.setCurrentItem(3);
    }
}
